package com.acme.bankapp.domain.bank;

import java.util.Locale;

public class CheckingAccount extends AbstractAccount {
	protected double overdraft;
	
	public CheckingAccount(double balance, double overdraft) {
		super(balance);
		this.overdraft = overdraft;
	}

	public void withdraw(double amount) throws OverdraftLimitExceededException {
		if (amount <= 0) {
			throw new IllegalArgumentException(MSG_AMOUNT_NONPOSITIVE);
		} else if (amount > this.balance + this.overdraft) {
			throw new OverdraftLimitExceededException(this);
		} else {
			balance -= amount;
		}

		assert (balance > -overdraft);
	}

	public double getMaximumAmountToWithdraw() {
		return balance + overdraft;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(balance);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(overdraft);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CheckingAccount other = (CheckingAccount) obj;
		if (Double.doubleToLongBits(balance) != Double
				.doubleToLongBits(other.balance))
			return false;
		if (Double.doubleToLongBits(overdraft) != Double
				.doubleToLongBits(other.overdraft))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return String.format(Locale.ENGLISH, "%s, overdraft = %.2f", super.toString(), this.overdraft);
	}
	
}
